package com.bac.rentmap.ad.flat;

import java.io.Serializable;

public class LocalAddress implements Serializable {
    private LocalAddressPrefix prefix;
    private String street;
    private Integer building;
    private String buildingPart;

    public LocalAddress(LocalAddressPrefix prefix, String street,
            Integer building) {
        this.prefix = prefix;
        this.street = street;
        this.building = building;
    }

    public LocalAddress(LocalAddressPrefix prefix, String street,
            Integer building, String buildingPart) {
        this.prefix = prefix;
        this.street = street;
        this.building = building;
        this.buildingPart = buildingPart;
    }

    public LocalAddressPrefix getPrefix() {
        return prefix;
    }

    public String getStreet() {
        return street;
    }

    public Integer getBuilding() {
        return building;
    }

    @Override
    public String toString() {
        String streetString = getStreetString();

        if (building != null) {
            String res = streetString + ", " + building;
            if (buildingPart != null) {
                try {
                    Integer buildingPartNumber = Integer.valueOf(buildingPart);
                    return res + " корп. " + buildingPartNumber;
                } catch (NumberFormatException e) {
                    return res + buildingPart;
                }
            }
            return res;
        } else
            return streetString;
    }

    public String getStreetString() {
        if (prefix == null)
            return street;
        else
            return prefix.getName() + " " + street;
    }

    public String getBuildingPart() {
        return buildingPart;
    }

    public void setBuilding(Integer building) {
        this.building = building;
    }

    public void setBuildingPart(String buildingPart) {
        this.buildingPart = buildingPart;
    }

}
